<?php

/**
 * 首页
 * david.yan (david.yan@qq.com) by 2013
 * 北京泰和盈动科技有公司 版权所有
 */

class user extends actionAbstract{
    public $title = '';
    public $css = array();
    public $modules = array();
    public $menu = array();
    public $pos = 1;
	public $type=0;
	public $leftpos=0;
    public $where='';
	/**
	* 递归要添加的菜单PIN
	 *
	 * @var array
	 */
	protected $add_auth = array ();
    protected $menudatas = array ();
	
	
    /**
     * 构造
     */
    function __construct(){
        parent::__construct();
        $this->info = acl::checkLogin('admininfo',$this->url('common/login'));
         // if(!isset($_SESSION['accessinfo'])){ header('location:'.$this->url('common/login'));}
		$this->menu = $this->loadConfig();
		$this->type = isset($_GET['type'])?(int)$_GET['type']:0;
    }
    
    //商家列表
    public function index(){
		$this->leftpos=0;
        $this->loadModel('pms','account');
		
		$page=isset($_GET['page'])?(int)($_GET['page']):1;
        $nameKey=isset($_GET['nameKey'])?$_GET['nameKey']:'';
		
		$sql_from=" from pms_account";
		$sql_where=" where isbusiness=2";
		$sql_order_by=" order by lastTime desc";
		$field_reto=" count(id)";
		$field_re=" *";
		
		if($nameKey){
            $nameKey=trim($nameKey);
            $nameKeyArr=explode(',',$nameKey);
            foreach($nameKeyArr as $k=>$v){
                $sql_where.=" and (userName like '%".trim($v)."%' or hotelName like '%".trim($v)."%')";
            }
        }
		
		$return=$this->getPage($sql_from,$sql_where,$sql_order_by,$field_reto,$field_re,"pms","account",$page);
		$re=$return['re'];$number=$return['number'];$pageHtml=$return['pageHtml'];
		
		if($re){
			foreach($re as $k=>$v){
				if($v['parentid']!=0){
					$re[$k]['userName']=$v['userName']."（子账号）";
					$parentName=$this->pms->accountModel->fetchRow("select hotelName from pms_account where id=".$v['parentid']);
					if($parentName){$re[$k]['hotelName']=$parentName['hotelName'];}
				}
			}
		}
        
        include $this->loadWidget('masterTheme');
    }
    //添加商家
    public function edit(){
		$this->leftpos=0;
        $this->loadHelper('extend');
        $this->loadModel('pms','account');
        $this->loadModel('hotel','category');
        $this->loadModel('tc','province');
        $this->loadModel('tc','city');
        $this->loadModel('tc','county');
        
        $province=$this->tc->provinceModel->fetchAll("select * from tc_province");
        
        if($_POST){
            $province=isset($_POST['province'])?(int)$_POST['province']:0;
            $city=isset($_POST['city'])?(int)$_POST['city']:0;
            $county=isset($_POST['county'])?(int)$_POST['county']:0;
            $address=isset($_POST['address'])?$_POST['address']:'';
			
            if(!isset($_POST['userName'])||$_POST['userName']==''){
                ajaxReturn ('','请填写账号！', 0 );exit;
            }
            if(!isset($_POST['hotelName'])||$_POST['hotelName']==''){
                ajaxReturn ('','请填写酒店名称！', 0 );exit;
            }
            if(empty($province)){
                ajaxReturn ('','请选择省！', 0 );exit;
            }
            if(empty($city)){
                ajaxReturn ('','请选择市！', 0 );exit;
            }
            if(empty($county)){
                ajaxReturn ('','请选择区！', 0 );exit;
            }
            if(!isset($_POST['address'])||$_POST['address']==''){
                ajaxReturn ('','请填写酒店地址！', 0 );exit;
            }
            
            $data['userName']=isset($_POST['userName'])?$_POST['userName']:'';
            $data['status']=1;
            $data['password']=md5(123456);
            $data['created']=time();
            $data['hotelName']=isset($_POST['hotelName'])?$_POST['hotelName']:'';
            $data['lastIp']='';
			
            $resouce=$this->pms->accountModel->fetchRow("select id from pms_account where userName='".$_POST['userName']."'");
            if($resouce){
                ajaxReturn ('','该账号已经存在！', 0 );exit;
            }else{
                $re=$this->pms->accountModel->insert($data);
                if($re){
                    //添加酒店信息
                    $arr['hotelName']=$_POST['hotelName'];
                    $arr['partnerid']=$re;
                    $arr['province']=$province;
                    $arr['city']=$city;
                    $arr['county']=$county;
                    $arr['address']=$address;
                    $arr['status']=1;
                    $hotelarr=$this->hotel->categoryModel->insert($arr);
                    //传输给OSII
                    $provinceName=$this->tc->provinceModel->fetchRow("select name from tc_province where osiiid=$province");
                    $cityName=$this->tc->cityModel->fetchRow("select name from tc_city where osiiid=$city");
                    $countyName=$this->tc->countyModel->fetchRow("select name from tc_county where osiiid=$county");
                    
                    $this->loadApi('osiiHotel');
                    $osiiHotel=new osiiHotel();
                    
                    $body=array();
                    $body[0]['hotelCode']=$hotelarr;               		$body[0]['hotelName']=$_POST['hotelName'];
					$body[0]['hotelEnName']="";			           		$body[0]['hotelDescription']="";
					$body[0]['star']=0;						       		$body[0]['provinceCode']=$province;
					$body[0]['provinceName']=$provinceName['name'];		$body[0]['cityCode']=$city;
					$body[0]['cityName']=$cityName['name'];				$body[0]['countyCode']=$county;
					$body[0]['countyName']=$countyName['name'];			$body[0]['brief']="";
					$body[0]['openYear']="2000-01-01";					$body[0]['roomQuantity']=0;
					$body[0]['telephone']="";							$body[0]['diyBreakfast']=0;
                    $body[0]['chineseBreakfast']=0;						$body[0]['westBreakfast']=0;
					$body[0]['fax']="";									$body[0]['address']=$address;
                    //echo "<pre>";print_r($body);exit;
                    $reosii=$osiiHotel->hotels($body);
                    
                    ajaxReturn ('back','操作成功！', 1 );exit;
                }else{
                    ajaxReturn ('','操作失败！', 0 );exit;
                }
            }
        }
        
        include $this->loadWidget('masterTheme');
    }
    //选择省 获取市  选择市 获取区
    function get_address(){
        $this->loadModel('tc','city');
        $this->loadModel('tc','county');
        $parentid=isset($_POST['parentid'])?(int)$_POST['parentid']:0;
        $type=isset($_POST['type'])?(int)($_POST['type']):0;
        if($type==1){
            $re=$this->tc->cityModel->fetchAll("select * from tc_city where province_id=$parentid");
        }elseif($type==2){
            $re=$this->tc->countyModel->fetchAll("select * from tc_county where city_id=$parentid");
        }
        $html="";
        $html.="<option value='0'>请选择</option>";
        if(!empty($re)){
            foreach($re as $k=>$v){
                $html.="<option value='".$v['osiiid']."'>".$v['name']."</option>";
            }
        }
        exit(json_encode(array('type'=>$type,'html'=>$html)));
    }
    
    //修改商家状态
    public function status(){
        $this->loadModel('pms','account');
        $this->loadModel('hotel','category');
        $this->loadHelper('extend');
        if($_GET){
            $id=isset($_GET['id'])?$_GET['id']:0;
            $result=$this->pms->accountModel->fetchRow("select * from pms_account where id=$id");
            if($result&&$result['status']==1){
                $data['status']=2;
            }elseif($result&&$result['status']==2){
                $data['status']=1;
            }
            $re=$this->pms->accountModel->update($data,$id);
            if($re){
                $this->hotel->categoryModel->update($data,"partnerid=$id");
                ajaxReturn ('back','修改成功！', 1 );exit;
            }else{
                ajaxReturn ('','修改失败！', 0 );exit;
            }
        }
    }
	
    //包房商列表
    public function business(){
    	$this->leftpos=7;
    	$this->loadModel('pms','account');
		
		$page=isset($_GET['page'])?(int)($_GET['page']):1;
        $nameKey=isset($_GET['nameKey'])?$_GET['nameKey']:'';
		
		
		$sql_from=" from pms_account";
		$sql_where=" where isbusiness=1";
		$sql_order_by=" order by lastTime desc";
		$field_reto=" count(id)";
		$field_re=" *";
		
		if($nameKey){
            $nameKey=trim($nameKey);
            $nameKeyArr=explode(',',$nameKey);
            foreach($nameKeyArr as $k=>$v){
                $sql_where.=" and (userName like '%".trim($v)."%' or hotelName like '%".trim($v)."%')";
            }
        }
		
		$return=$this->getPage($sql_from,$sql_where,$sql_order_by,$field_reto,$field_re,"pms","account",$page);
		$re=$return['re'];$number=$return['number'];$pageHtml=$return['pageHtml'];
		
		if($re){
			foreach($re as $k=>$v){
				if($v['parentid']!=0){
					$re[$k]['userName']=$v['userName']."（子账号）";
					$parentName=$this->pms->accountModel->fetchRow("select hotelName,another from pms_account where id=".$v['parentid']);
					if($parentName){$re[$k]['hotelName']=$parentName['hotelName'];$re[$k]['another']=$parentName['another'];}
				}
			}
		}
		
    	include $this->loadWidget('masterTheme');
    }
	
    //添加包房商
    public function editbusiness(){
    	$this->leftpos=7;
        global $pmsconfigs;
    	$this->loadHelper('extend');
    	$this->loadModel('pms','account');
    	$this->loadModel('tc','province');
    
    	$province=$this->tc->provinceModel->fetchAll("select * from tc_province");
    
    	if($_POST){
    		$province=isset($_POST['province'])?(int)$_POST['province']:0;
    		$city=isset($_POST['city'])?(int)$_POST['city']:0;
    		$county=isset($_POST['county'])?(int)$_POST['county']:0;
    		$address=isset($_POST['address'])?$_POST['address']:'';
            
            if(!isset($_POST['userName'])||$_POST['userName']==''){
                ajaxReturn ('','请填写账号！', 0 );exit;
            }
            if(!isset($_POST['hotelName'])||$_POST['hotelName']==''){
                ajaxReturn ('','请填写公司名称！', 0 );exit;
            }
    		if(empty($province)){
    			ajaxReturn ('','请选择省！', 0 );exit;
    		}
    		if(empty($city)){
    			ajaxReturn ('','请选择市！', 0 );exit;
    		}
    		if(empty($county)){
    			ajaxReturn ('','请选择区！', 0 );exit;
    		}
            if(!isset($_POST['address'])||$_POST['address']==''){
                ajaxReturn ('','请填写公司地址！', 0 );exit;
            }
    		if(strlen($_POST['phone'])!=11||!in_array(substr($_POST['phone'],0,3),$pmsconfigs['phone'])){
    			ajaxReturn ('','请正确填写联系方式！', 0 );
    		}
            if(!isset($_POST['another'])||$_POST['another']==''){
                ajaxReturn ('','请填写紧急联系人！', 0 );exit;
            }
    		if(!preg_match( "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i", $_POST['email'] )){
    			ajaxReturn ('','请正确填写邮箱！', 0 );
    		}
			
    		$data['userName']=isset($_POST['userName'])?$_POST['userName']:'';
    		$data['status']=1;
    		$data['password']=md5(123456);
    		$data['created']=time();
    		$data['hotelName']=isset($_POST['hotelName'])?$_POST['hotelName']:'';
    		$data['phone']=isset($_POST['phone'])?$_POST['phone']:'';
    		$data['another']=isset($_POST['another'])?$_POST['another']:'';
    		$data['address']=isset($_POST['address'])?$_POST['address']:'';
    		$data['email']=isset($_POST['email'])?$_POST['email']:'';
    		$data['isbusiness']=1;
			
    		$resouce=$this->pms->accountModel->fetchRow("select id from pms_account where userName='".$_POST['userName']."'");
    		if($resouce){
    			ajaxReturn ('','该账号已经存在！', 0 );exit;
    		}else{
    			$re=$this->pms->accountModel->insert($data);
    			if($re){
    				ajaxReturn ('back','操作成功！', 1 );exit;
    			}else{
    				ajaxReturn ('','操作失败！', 0 );exit;
    			}
    		}
    	}
        
    	include $this->loadWidget('masterTheme');
    }
	
    //酒店列表
    public function hotel(){
		$this->leftpos=1;
		$this->loadModel('pms','devices');
		
		$page=isset($_GET['page'])?(int)($_GET['page']):1;
        $nameKey=isset($_GET['nameKey'])?$_GET['nameKey']:'';
		
		$sql_from=" from hotel_category as a left join tc_province as b on b.osiiid=a.province";
		$sql_from.=" left join tc_city as c on c.osiiid=a.city left join tc_county as d on d.osiiid=a.county";
		$sql_where=" where a.id>0";
		$sql_order_by=" order by a.id asc";
		$field_reto=" count(a.id)";
		$field_re=" a.*,b.name as pName,c.name as cName,d.name as dName";
		
		if($nameKey){
            $nameKey=trim($nameKey);
            $nameKeyArr=explode(',',$nameKey);
            foreach($nameKeyArr as $k=>$v){
                $sql_where.=" and (a.hotelName like '%".trim($v)."%' or a.tel like '%".trim($v)."%' or a.address like '%".trim($v)."%'";
				$sql_where.=" or b.name like '%".trim($v)."%' or c.name like '%".trim($v)."%' or d.name like '%".trim($v)."%'";
				$sql_where.=" or a.introduce like '%".trim($v)."%' or a.ambitus like '%".trim($v)."%')";
            }
        }
		
		$return=$this->getPage($sql_from,$sql_where,$sql_order_by,$field_reto,$field_re,"pms","account",$page,7);
		$re=$return['re'];$number=$return['number'];$pageHtml=$return['pageHtml'];
		
		if($re){
			foreach($re as $k=>$v){
				$re[$k]['devices']=array();
				$devices=$this->pms->devicesModel->fetchAll("select title from pms_devices where hotelId=".$v['id']);
				if($devices){$re[$k]['devices']=$devices['title'];}
			}
		}
		
        include $this->loadWidget('masterTheme');
    }
	
    //修改酒店状态
    public function hotelStatus(){
        $this->loadModel('hotel','category');
        $this->loadModel('pms','account');
        $this->loadHelper('extend');
        if($_GET){
            $id=isset($_GET['id'])?$_GET['id']:0;
            $result=$this->hotel->categoryModel->fetchRow("select * from hotel_category where id=$id");
            $osiiStatus=0;
            if($result&&$result['status']==1){
                $data['status']=2;
                $osiiStatus=1;
            }elseif($result&&$result['status']==2){
                $data['status']=1;
            }
            $re=$this->hotel->categoryModel->update($data,$id);
            if($re){
                $this->pms->accountModel->update($data,$result['partnerid']);
                //传送 OSSII
                $this->loadApi('osiiHotel');
                $osiiHotel=new osiiHotel();
                $bodys=array();
                $bodys['hotelCode']=$result['id'];          $bodys['hotelStatus']=$osiiStatus;
                $hotelStatus_osii=$osiiHotel->setHotelStatus($bodys);
                
                ajaxReturn ('back','修改成功！', 1 );exit;
            }else{
                ajaxReturn ('','修改失败！', 0 );exit;
            }
        }
    }
    
    //房型
    function type(){
		$this->leftpos=2;
		
		$page=isset($_GET['page'])?(int)($_GET['page']):1;
        $nameKey=isset($_GET['nameKey'])?$_GET['nameKey']:'';
		
		$sql_from=" from hotel_type as a left join hotel_category as b on b.id=a.categoryid";
		$sql_where=" where a.parentTypeId=0";
		$sql_order_by=" order by a.categoryid,a.id asc";
		$field_reto=" count(a.id)";
		$field_re=" a.*,b.hotelName";
		
		if($nameKey){
            $nameKey=trim($nameKey);
            $nameKeyArr=explode(',',$nameKey);
            foreach($nameKeyArr as $k=>$v){
                $sql_where.=" and (a.title like '%".trim($v)."%' or a.bed like '%".trim($v)."%' or b.hotelName like '%".trim($v)."%')";
            }
        }
		
		$return=$this->getPage($sql_from,$sql_where,$sql_order_by,$field_reto,$field_re,"hotel","type",$page);
		$re=$return['re'];$number=$return['number'];$pageHtml=$return['pageHtml'];
		
        include $this->loadWidget('masterTheme');
    }
        
    //价格
    function price(){
		$this->leftpos=4;
        $this->loadModel('pms','channels');
		
		$channels=$this->pms->channelsModel->fetchAll("select title,osiiId from pms_channels");
		
		$page=isset($_GET['page'])?(int)($_GET['page']):1;
        $nameKey=isset($_GET['nameKey'])?$_GET['nameKey']:'';
		$channelsId=isset($_GET['channelsId'])?$_GET['channelsId']:0;
        $sta=isset($_GET['sta'])?$_GET['sta']:'';
        $end=isset($_GET['end'])?$_GET['end']:'';
		
		$sql_from=" from hotel_price as a left join hotel_category as b on b.id=a.hotelId left join pms_channels as c";
		$sql_from.=" on c.osiiId=a.channels left join hotel_type as d on d.id=a.typeid";
		$sql_where=" where a.isdetailed=1 and LOCATE(',',a.channels)<1";
		$sql_order_by=" order by a.hotelId,a.id,a.times asc";
		$field_reto=" count(a.id)";
		$field_re=" a.*,b.hotelName,c.title as channelsName,d.title";
		
		if($nameKey){
            $nameKey=trim($nameKey);
            $nameKeyArr=explode(',',$nameKey);
            foreach($nameKeyArr as $k=>$v){
                $sql_where.=" and (d.title like '%".trim($v)."%' or b.hotelName like '%".trim($v)."%') ";
            }
        }
		if($channelsId){
			$sql_where.=" and a.channels=$channelsId";
		}
		if($sta){
			$sql_where.=" and a.times>=".strtotime($sta);
		}
		if($end){
			$sql_where.=" and a.times<=".strtotime($end);
		}
		
		$return=$this->getPage($sql_from,$sql_where,$sql_order_by,$field_reto,$field_re,"hotel","price",$page);
		$re=$return['re'];$number=$return['number'];$pageHtml=$return['pageHtml'];
		
        include $this->loadWidget('masterTheme');
    }
	
    //浮动价格
    function floating(){
    	$this->leftpos=6;
		
		$page=isset($_GET['page'])?(int)($_GET['page']):1;
        $nameKey=isset($_GET['nameKey'])?$_GET['nameKey']:'';
        $sta=isset($_GET['sta'])?$_GET['sta']:'';
        $end=isset($_GET['end'])?$_GET['end']:'';
		
		$sql_from=" from float_log as a left join hotel_type as b on b.id=a.parent_TypeId left join hotel_category as c on c.id=b.categoryid";
		$sql_where=" where a.id>0";
		$sql_order_by=" order by a.hotelid,a.parent_TypeId,a.time asc";
		$field_reto=" count(a.id)";
		$field_re=" a.*,b.title,c.hotelName";
		
		if($nameKey){
            $nameKey=trim($nameKey);
            $nameKeyArr=explode(',',$nameKey);
            foreach($nameKeyArr as $k=>$v){
                $sql_where.=" and (c.hotelName like '%".trim($v)."%' or b.title like '%".trim($v)."%')";
            }
        }
		if($sta){
			$sql_where.=" and a.time>=".strtotime($sta);
		}
		if($end){
			$sql_where.=" and a.time<=".strtotime($end);
		}
		
		$return=$this->getPage($sql_from,$sql_where,$sql_order_by,$field_reto,$field_re,"float","log",$page);
		$re=$return['re'];$number=$return['number'];$pageHtml=$return['pageHtml'];
		
    	include $this->loadWidget('masterTheme');
    }
	
    //图片
    function picture(){
		$this->leftpos=5;
		
		$page=isset($_GET['page'])?(int)($_GET['page']):1;
        $nameKey=isset($_GET['nameKey'])?$_GET['nameKey']:'';
		
		$sql_from=" from hotel_picture as a left join hotel_type as b on b.id=a.typeid left join hotel_category as c on c.id=b.categoryid";
		$sql_where=" where a.id>0";
		$sql_order_by=" order by a.status asc";
		$field_reto=" count(a.id)";
		$field_re=" a.*,b.title,c.hotelName";
		
		if($nameKey){
            $nameKey=trim($nameKey);
            $nameKeyArr=explode(',',$nameKey);
            foreach($nameKeyArr as $k=>$v){
                $sql_where.=" and (c.hotelName like '%".trim($v)."%' or b.title like '%".trim($v)."%')";
            }
        }
		
		$return=$this->getPage($sql_from,$sql_where,$sql_order_by,$field_reto,$field_re,"hotel","picture",$page);
		$re=$return['re'];$number=$return['number'];$pageHtml=$return['pageHtml'];
		
        include $this->loadWidget('masterTheme');
    }
	
    //图片审核
    function pictureStatus(){
        $this->loadModel('hotel','picture');
        if($_POST){
            $id=isset($_POST['id'])?(int)($_POST['id']):0;
            $data['status']=isset($_POST['status'])?(int)($_POST['status']):0;
            $re=$this->hotel->pictureModel->update($data,$id);
            if($re){
                if($data['status']==1){
                    //传输给OSII
                    $ims=$this->hotel->pictureModel->fetchRow("select * from hotel_picture where id=$id");
                    
                    $this->loadApi('osiiHotel');
                    $osiiHotel=new osiiHotel();
                    
                    $body=array();
                    $body[0]['hotelCode']=$ims['hotelId'];		$body[0]['picName']=$ims['title'];
					$body[0]['picUrl']=$_SERVER['HTTP_HOST'].$ims['pcurl'];		$body[0]['licensedDownload']=0;
                    
                    $reosii=$osiiHotel->imgs($body);
                }
                
                exit(json_encode(array('state'=>1,'html'=>'操作成功！')));
            }else{
                exit(json_encode(array('state'=>0,'html'=>'操作失败！')));
            }
        }
    }
    
    
}
?>